#include <stdio.h>
#include <string.h>
#include <stdlib.h>


/*int main() {
    char str1[100]; 
    gets(str1);
    char **p1;
    p1 = strcut(str1);

    for (int i = 0; p1[i] != NULL; i++)
        printf("p1[%d] = %s\n", i, p1[i]);
    putchar('\n');

  
    // 释放内存
   
    free(p1);
    return 0;
}
*/

char **strcut(const char *source) {
	char flag = ' ';
    char **pt;
    int j, n = 0;
    int count = 1;
    int len = strlen(source);
    char tmp[len + 1];
    tmp[0] = '\0';

    
    for (int i = 0; i < len; ++i)  {
        if (source[i] == flag && source[i+1] == '\0') {
            continue;
        }    
        else if (source[i] == flag && source[i+1] != flag) {
            count++;
        }
    }
    // 多分配一个char*，是为了设置结束标志
    pt = (char**)malloc((count+1) * sizeof(char*));

    count = 0;
    for (int i = 0; i < len; ++i) {
        if (i == len - 1 && source[i] != flag)
        {
            tmp[n++] = source[i];
            tmp[n] = '\0';  // 字符串末尾添加空字符
            j = strlen(tmp) + 1;
            pt[count] = (char*)malloc(j * sizeof(char));
            strcpy(pt[count++], tmp);
        }
        else if (source[i] == flag) {
            j = strlen(tmp);
            if (j != 0) {
                tmp[n] = '\0';  // 字符串末尾添加空字符
                pt[count] = (char*)malloc((j+1) * sizeof(char));
                strcpy(pt[count++], tmp);
                // 重置tmp
                n = 0;
                tmp[0] = '\0';
            }
        }
        else {
            tmp[n++] = source[i];
        }
    }
    // 设置结束标志
    pt[count] = NULL;

    return pt;
}

int which_request(char *request){		//通过字符串判断是那条命令
	if(!strcmp(request,"login")){
		return 1;
	}
	else if(!strcmp(request,"register")){
		return 2;
	}
	else if(!strcmp(request,"send")){
		return 3;
	}
	else if(!strcmp(request,"search")){
		return 4;
	}
	else if(!strcmp(request,"back")){
		return 5;
	}
	else if(!strcmp(request,"update")){
		return 6;
	}
	else if(!strcmp(request,"ccs")){
		return 7;
	}
	else if(!strcmp(request,"quit")){
		return 8;
	}
	else if(!strcmp(request,"addrBook")){
		return 9;
	}
	else return 10;
}

void Execute_the_order(const char *request){
	char **ts;
	ts =  strcut(request);
	switch(which_request(*ts)){		//通过返回值来执行相应操作
	case 1:login(ts[1],ts[2],ts[3]);break;
	case 2:REGISTER(ts[1],ts[2],ts[3],ts[4]);break;
	case 3:send(ts[1],ts[2]);break;
	case 4:search(ts[1],ts[2],ts[3]);break;
	case 5:back(ts[1],ts[2]);break;
	case 6:update(ts[1],ts[2],ts[3],ts[4]);break;
	case 7:css(ts[1],ts[2]);break;
	case 8:quit(ts[1]);break;
	case 9:assrBook(st[1]);break;
	case 10:printf("1\n");break;
	}
}

void main(void){
	char a[]="login 123 123"; 
	Execute_the_order(a);
}
